import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @auther zhouliangliang
 * @date 2022/10/9 9:39
 * @description
 */
public class Foo {

    private void iterator(List<String> list) {
        list.stream().forEach(item->{
            System.out.println(item);
        });
    }

    //滑动窗口,计算最长子串
    public int lengthOfLongestSubstring(String s) {
        int[][] arr = new int[][]{};
        Arrays.sort(arr, new Comparator<int[]>(){
            public int compare(int[] int1, int[] int2) {
                return int1[0] > int2[0] ? 1: 0;
            }
        });

        Arrays.sort(arr, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return 0;
            }
        });
        int length = 0;
        List<Character> list = new ArrayList<>();
        for(int i=0; i<s.length(); i++) {
            char ch = s.charAt(i);
            if(!list.contains(ch)) {
                list.add(ch);
            } else {
                length = length > list.size() ? length : list.size();
                while(list.contains(ch)) {
                    list.remove(0);
                }
                list.add(ch);
            }
        }
        return length > list.size() ? length : list.size();
    }

    //螺旋填充数组
    public int[][] generateMatrix(int n) {
        int left = 0;
        int right = n-1;
        int top = 0;
        int bottom = n-1;
        int start = 1;
        int max = n * n;
        int[][] matrix = new int[n][n];
        while(start <= max) {
            //从左到右填充上边的行，行不变，列递增
            for(int i=left; i<=right; i++) {
                matrix[top][i] = start ++;
            }
            top = top + 1;//填充过一行了

            //从上到下填充右边的列，列不变，行递增
            for(int i=top; i<=bottom; i++) {
                matrix[i][right] = start ++;
            }
            right = right -1;//填充过右侧列了

            //从右到左填充下边的行，行不变，列递减
            for(int i=right; i>=left; i--) {
                matrix[bottom][i] = start++;
            }
            bottom = bottom - 1;//填充过下侧行了

            //从下到上填充左侧的列，列不变，行递减
            for(int i=bottom; i>=top; i--) {
                matrix[i][left] = start++;
            }
        }
        return matrix;
    }

    //数字反转
    public int reverseNum(int value) {
        int num = 0;
        while(value != 0) {
            if(num * 10 > Integer.MAX_VALUE || num * 10 < Integer.MIN_VALUE) {
                return -1;
            }
            int y = value % 10;
            value = value /10;
            num = num * 10 + y;
        }
        return num;
    }


    public int longestConsecutive(int[] nums) {
        if(nums.length == 0){
            return 0;
        }
        // 自增排序
        Arrays.sort(nums);
        // 标记序列数 默认为1
        int flag = 1;
        // 最长序列数 默认为1
        int max = 1;
        for(int i=0; i<nums.length-1; i++){
            if(nums[i+1] - nums[i] == 0){
                continue;
            }
            if(nums[i+1] - nums[i] == 1){
                flag++;
            }else{
                flag = 1;
            }
            max = flag > max ? flag : max;
        }
        return max;
    }

    /**
     * 数组合并
     * [[1,3],[2,6],[8,10],[15,18]]
     * [[1,6],[8,10],[15,18]]
     * @param args
     */
    public int[][] merge(int[][] intervals) {
            //1. 判空
            //2. 排序
            //3. 合并
            if(intervals.length == 0) {
                return new int[0][2];
            }

            Arrays.sort(intervals, new Comparator<int[]>(){
                public int compare(int[] int1, int[] int2) {
                    return int1[0] - int2[0];
                }
            });

            List<int[]> mergeList = new ArrayList<>();

            for(int i=0; i<intervals.length; i++) {
                int left = intervals[i][0];
                int right = intervals[i][1];
                if(mergeList.size() == 0 || mergeList.get(mergeList.size()-1)[1] < left) {
                    mergeList.add(new int[]{left, right});
                } else {
                    mergeList.get(mergeList.size()-1)[1] = Math.max(mergeList.get(mergeList.size()-1)[1], right);
                }
            }
            return mergeList.toArray(new int[mergeList.size()][]);
    }
    public static void main(String[] args) {
//        int length = new Foo().lengthOfLongestSubstring("abcabcbb");
//        System.out.println(length);
//        System.out.println(new Foo().reverseNum(-123));
        int[][] intervals = new int[][]{{1,4},{0,4}};
        new Foo().merge(intervals);
    }
}
